const express = require('express')
const router = express.Router()
const passport = require('api/passport')
const houseService = require('services/house')
const orderService = require('services/order')
const reviewService = require('services/review')
const filterService = require('services/filter')
const bookeDateService = require('services/bookedDate')

router.get('/', async (req, res, next) => {
  try {
    let query = req.query
    let { searchType } = query;
    ['year', 'month', 'day', 'hour'].forEach((key) => {
      if (key == searchType) {
        query[key + 'Enabled'] = 1
      }
    })
    console.log('query', query)
    res.json(await houseService.query(query, req.query.page))
  } catch (e) {
    next(e)
  }
})

router.get('/filters', async (req, res, next) => {
  try {
    const filters = await filterService.get()
    res.json(filters)
  } catch (e) {
    next(e)
  }
})

router.get('/selected', async (req, res, next) => {
  try {
    res.json(await houseService.getSelected())
  } catch (e) {
    next(e)
  }
})

router.get('/:id/invalidDate', async (req, res) => {
  const {year, month} = req.query
  const houseId = req.params.id
  let days = await bookeDateService.invalidDaysWithMonth(houseId, year, month)
  res.json(days)
  // res.json(await houseService.get(req.params.id))
})

router.get('/:id/bookedTime', async (req, res, next) => {
  const {date} = req.query
  const houseId = req.params.id
  if (!date) {
    next(new BizError('no book date'))
    return
  }
  if (!houseId) {
    next(new BizError('no houseId'))
    return
  }
  // const house = await houseService.get(houseId)
  const house = await houseService.get(houseId)


  let hours = []
  // 空间当日关闭
  let [ year, month ] = date.split('/')
  let days = await bookeDateService.invalidDaysWithMonth(houseId, year, month)
  if (days.indexOf(date) >= 0) {
    for (i = 0 ; i < 24; i++) {
      hours.push(i)
    }
    res.json(hours)
    return
  }

  // 开关时间
  let orders = await orderService.getOrdersByDate(houseId, new Date(date))
  orders = orders.toJSON()
  console.log('orders', orders)
  for (i = 0; i < 24; i++) {
    let startTime = i
    let endTime = i + 1
    let compareStart = new Date(`${date} ${startTime || 0}:00`)
    let compareEnd = new Date(`${date} ${endTime || 24}:00`)
    let oneHour = 3600 * 1000
    if (orders.some((order) => {
      return +order.startTime - oneHour <= +compareStart && +order.endTime + oneHour >= +compareEnd
    })) {
      hours.push(i)
    }
  }

  // 已预订订单
  res.json(hours)
})

router.get('/:id/reviews', passport.auth, async (req, res, next) => {
  try {
    res.json(await reviewService.getReviewList(req.params.id, req.user && req.user.id, req.query.pageIndex))
  } catch (e) {
    next(e)
  }
})

router.get('/:id/selectedReviews', async (req, res, next) => {
  try {
    res.json(await reviewService.getSelectedList(req.params.id))
  } catch (e) {
    next(e)
  }
})

router.get('/:id', async (req, res) => {
  res.json(await houseService.get(req.params.id))
})

module.exports = router